package com.simon.adapter;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

import com.cmonbaby.adapter.recycleview.RCommonAdapter;
import com.cmonbaby.adapter.recycleview.RMultiItemTypeAdapter;
import com.cmonbaby.adapter.recycleview.RViewHolder;
import com.cmonbaby.ioc.core.annotation.ContentView;
import com.simon.adapter.base.BaseRViewActivity;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_recyclerview)
public class RecyclerViewActivity extends BaseRViewActivity {

    private List<String> mDatas = new ArrayList<>();
    private RCommonAdapter<String> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDatas();

        mAdapter.setOnItemClickListener(new RCommonAdapter.OnItemClickListener<String>() {
            @Override
            public void onItemClick(View view, String data, int position) {
                Toast.makeText(RecyclerViewActivity.this, data, Toast.LENGTH_SHORT).show();
                mAdapter.removeDatas(position);
                helper.getLoadMoreWrapper().notifyDataSetChanged();
            }
        });

        mAdapter.setOnItemLongClickListener(new RCommonAdapter.OnItemLongClickListener<String>() {
            @Override
            public boolean onItemLongClick(View view, String data, int position) {
                Toast.makeText(RecyclerViewActivity.this, "onItemLongClick = " + position, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    private void initDatas() {
        if (mDatas != null && mDatas.size() != 0) {
            mDatas.clear();
        }
        for (int i = 'A'; i <= 'z'; i++) {
            if (mDatas != null) mDatas.add((char) i + "");
        }
        notifyAdapterDataSetChanged(mDatas);
    }

    @Override
    public int[] colorRes() {
        return new int[0];
    }

    @Override
    public RMultiItemTypeAdapter createRecycleViewAdapter() {
        mAdapter = new RCommonAdapter<String>(R.layout.item_list, mDatas) {
            @Override
            protected void convert(RViewHolder holder, String s, int position) {
                holder.setText(R.id.id_item_list_title, s + " : " + holder.getAdapterPosition());
            }
        };
        return mAdapter;
    }

    @Override
    public void onRefresh() {
        initDatas();
    }

    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 9; i++) {
                    mDatas.add("Add:" + i);
                }
                helper.getLoadMoreWrapper().notifyDataSetChanged();
            }
        }, 1000);
    }

    @Override
    public boolean isSupportPaging() {
        return true;
    }
}
